home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / detexinfo.el.z / detexinfo.el
Encoding:
Text File  |  1998-05-21  |  10.3 KB  |  276 lines

  1. ;;; detexinfo.el --- remove Texinfo commands from a Texinfo source file
  2.  
  3. ;; Copyright (C) 1992-1993 Free Software Foundation, Inc.
  4.  
  5. ;; Keywords: tex, docs
  6.  
  7. ;; This file is part of XEmacs.
  8.  
  9. ;; XEmacs is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; XEmacs is distributed in the hope that it will be useful, but
  15. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. ;; General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with XEmacs; see the file COPYING.  If not, write to the 
  21. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. ;; Boston, MA 02111-1307, USA.
  23.  
  24. ;;; Synched up with: Not in FSF.
  25.  
  26. ;;; Here is a handy keybinding: 
  27.  
  28. (global-set-key "\C-x\\" 'detexinfo)
  29.  
  30. ;;;;;;;;;;;;;;;; detexinfo.el ;;;;;;;;;;;;;;;;
  31. ;;;
  32. ;;; Remove Texinfo commands from a Texinfo source file.
  33. ;;;
  34. ;;; Copyright (C) 1991, 1992 Free Software Foundation
  35. ;;; Robert J. Chassell
  36. ;;; bugs to bug-texinfo@prep.ai.mit.edu
  37. ;;;
  38. ;;; ==> test version <==
  39. ;;; Fails if Texinfo source file contains formatting errors.
  40. ;;; 
  41. ;;; Version 0.05 -  3 Jun 1992
  42. ;;; Add to list of removed commands.  Improve messages.
  43. ;;;
  44. ;;; Version 0.04 - 27 Jan 1992
  45. ;;; Rewrite to insert detexinfo'd text into a temporary buffer.
  46. ;;; 
  47. ;;; Version 0.03 - 27 Dec 1991
  48. ;;; Improved messages.
  49. ;;;
  50. ;;; Version 0.02 - 13 Nov 1991
  51. ;;; detexinfo-remove-inline-cmd, detexinfo-syntax-table: Handle 
  52. ;;;          nested commands.  
  53. ;;; detexinfo: Handle nested @'s, eg @samp{@}} and @samp{@@};
  54. ;;;          replace @TeX{} with TeX.
  55. ;;; 
  56. ;;; Version 0.01 - 13 Nov 1991
  57. ;;;
  58. ;;; Based on detex.el, by Bengt Martensson, 4 Oct 1987
  59. ;;;
  60. ;;;;;;;;;;;;;;;;
  61.  
  62. (defvar detexinfo-buffer-name "*detexinfo*"
  63.   "*Name of the temporary buffer used by \\[detexinfo].")
  64.  
  65. (defvar detexinfo-syntax-table nil)
  66.  
  67. (if detexinfo-syntax-table
  68.     nil
  69.   (setq detexinfo-syntax-table (make-syntax-table))
  70.   (modify-syntax-entry ?\[ "." detexinfo-syntax-table)
  71.   (modify-syntax-entry ?\] "." detexinfo-syntax-table)
  72.   (modify-syntax-entry ?\" "." detexinfo-syntax-table)
  73.   (modify-syntax-entry ?\\ "." detexinfo-syntax-table)
  74.   (modify-syntax-entry ?\( "." detexinfo-syntax-table)
  75.   (modify-syntax-entry ?\) "." detexinfo-syntax-table)
  76.   (modify-syntax-entry ?{ "(}" detexinfo-syntax-table)
  77.   (modify-syntax-entry ?} "){" detexinfo-syntax-table))
  78.  
  79. (defun detexinfo ()
  80.   "Remove Texinfo commands from current buffer, copying result to new buffer.
  81. BUG: Fails if Texinfo source file contains formatting errors."
  82.   (interactive)
  83.   (let ((input-buffer (current-buffer)))
  84.     ;; Find a buffer to use.
  85.     (switch-to-buffer (get-buffer-create detexinfo-buffer-name))
  86.     (setq major-mode 'detexinfo-mode)
  87.     (set-syntax-table detexinfo-syntax-table)
  88.     (erase-buffer)
  89.     (insert-buffer-substring input-buffer)
  90.     
  91.     ;; Replace @{ and @} with %#* and *#% temporarily, so @samp{@{} works.
  92.     ;; What is a better way of doing this??
  93.     (goto-char (point-min))
  94.     (while (search-forward "@{" nil t)  ; e.g., @samp{@{}
  95.       (replace-match "%#*"))
  96.     (goto-char (point-min))
  97.     (while (search-forward "@}" nil t)
  98.       (forward-char -3)                 ; e.g.,  @samp{@@}
  99.       (if (looking-at "@")              ; Two @@ in a row
  100.           (progn
  101.             (delete-char 2)
  102.             (insert "%&%#"))
  103.         (forward-char 1)
  104.         (delete-char 2)
  105.         (insert "*#%")))
  106.     
  107.     (goto-char (point-min))
  108.     ;; Remove @refill, the only inline command without braces.
  109.     (while (search-forward "@refill" nil t)
  110.       (replace-match ""))
  111.     ;; Replace @TeX{} with TeX
  112.     (goto-char (point-min))
  113.     (while (search-forward "@TeX{}" nil t) (replace-match "TeX" t t))
  114.     
  115.     (detexinfo-remove-line-cmds-without-arg)
  116.     (detexinfo-remove-inline-cmds-without-arg)
  117.     (detexinfo-remove-inline-cmds-keep-arg)
  118.     (detexinfo-remove-line-cmds-deletable-arg)
  119.     (detexinfo-remove-line-cmds-maybe-delete-arg)
  120.     (detexinfo-remove-line-cmds-keep-arg)
  121.     
  122.     ;; Now replace %#*, *#%, and %&%#  with {,  }, and @@.
  123.     (goto-char (point-min))
  124.     (while (search-forward "%#*" nil t)
  125.       (replace-match "{"))
  126.     (goto-char (point-min))
  127.     (while (search-forward "*#%" nil t)
  128.       (replace-match "}"))
  129.     (goto-char (point-min))
  130.     (while (search-forward "%&%#" nil t)
  131.       (replace-match "@@"))
  132.     
  133.     ;; Scan for remaining two character @-commands
  134.     (goto-char (point-min))
  135.     (while (search-forward "@" nil t)
  136.       (cond ((looking-at "[*:]")
  137.              (delete-region (1- (point)) (1+ (point))))
  138.             ((looking-at "[{}^@.'`]\"?!")
  139.              (delete-region (1- (point)) (point)))))
  140.     
  141.     (goto-char (point-min))
  142.     (message "Done...removed Texinfo commands from buffer. You may save it.")))
  143.  
  144. (defun detexinfo-remove-whole-line (cmd)
  145.   "Delete Texinfo line command CMD at beginning of line and rest of line."
  146.   (goto-char (point-min))
  147.   (while
  148.       (re-search-forward
  149.        (concat "^@" cmd "[ \n]+") (point-max) t)
  150.     (goto-char (match-beginning 0))
  151.     (delete-region
  152.      (point) (save-excursion (end-of-line) (1+ (point))))))
  153.  
  154. (defun detexinfo-remove-inline-cmd (cmd)
  155.   "Delete Texinfo inline command CMD, eg. @point, @code."
  156.   (goto-char (point-min))
  157.   (while
  158.       (re-search-forward (concat "@" cmd "{") (point-max) t)
  159.     (save-excursion
  160.       (forward-char -1)
  161.       (forward-sexp 1)
  162.       (delete-char -1))                 ; delete right brace
  163.     (delete-region (point) (match-beginning 0)))) 
  164.  
  165. ;;;;;;;;;;;;;;;; 
  166.  
  167. ;;; 1. @setfilename and other line commands with args to delete
  168.  
  169. (defvar detexinfo-line-cmds-deletable-arg
  170.   '("enumerate" "ftable" "vtable" "itemize" "table"
  171.     "setfilename" "settitle" "setchapternewpage" 
  172.     "footnotestyle" "paragraphindent"
  173.     "include" "need" "sp" 
  174.     "clear" "ifclear" "ifset"  "set"
  175.     "defcodeindex" "defindex" "syncodeindex" "synindex")
  176.   "List of Texinfo commands whose arguments should be deleted.")
  177.  
  178. (defun detexinfo-remove-line-cmds-deletable-arg ()
  179.   "Delete Texinfo line commands together with their args, eg @setfilename."
  180.   (message "Removing commands such as @enumerate...with their arguments...")
  181.   (mapcar 'detexinfo-remove-whole-line
  182.           detexinfo-line-cmds-deletable-arg))
  183.  
  184. ;;; 2. @cindex and other cmds with args that may be deleted
  185. ;;;    This list is here just to make it easier to revise the
  186. ;;;    categories.  In particular, you might want to keep the index entries.
  187.  
  188. (defvar detexinfo-line-cmds-maybe-delete-arg
  189.    '("cindex" "findex" "kindex" "pindex" "tindex" "vindex" "node"
  190.      "c" "comment" "end" "headings"  "printindex" "vskip" 
  191.      "evenfooting"  "evenheading" "everyfooting" "everyheading" 
  192.      "oddfooting" "oddheading")
  193.   "List of Texinfo commands whose arguments may possibly be deleted.")
  194.  
  195. (defun detexinfo-remove-line-cmds-maybe-delete-arg ()
  196.   "Delete Texinfo line commands together with their arguments, eg, @cindex."
  197.   (message "Removing commands such as @cindex...with their arguments...")
  198.   (mapcar 'detexinfo-remove-whole-line
  199.           detexinfo-line-cmds-maybe-delete-arg))
  200.  
  201. ;;; 3. @chapter and other line cmds with args to keep.
  202.  
  203. (defvar detexinfo-line-cmds-keep-arg
  204.    '("top" "chapter" "section" "subsection" "subsubsection" 
  205.      "unnumbered" "unnumberedsec" "unnumberedsubsec" "unnumberedsubsubsec" 
  206.      "majorheading" "chapheading" "heading" "subheading" "subsubheading" 
  207.       "appendix" "appendixsec" "appendixsubsec" "appendixsubsubsec" 
  208.      "item" "itemx"
  209.      "title" "subtitle" "center" "author" "exdent"  
  210.      "defcv" "deffn" "defivar" "defmac" "defmethod" "defop" "defopt" 
  211.      "defspec" "deftp" "deftypefn" "deftypefun" "deftypvr"
  212.      "deftypevar" "defun" "defvar" "defvr")
  213.   "List of Texinfo line commands whose arguments should be kept.")
  214.  
  215. (defun detexinfo-remove-line-cmds-keep-arg ()
  216.   "Delete Texinfo line commands but keep their arguments, eg @chapter."
  217.   (message "Removing commands such as @chapter...but not their arguments...")
  218.   (mapcar 'detexinfo-remove-line-cmd-keep-arg
  219.           detexinfo-line-cmds-keep-arg))
  220.  
  221. (defun detexinfo-remove-line-cmd-keep-arg (cmd)
  222.   "Delete Texinfo line command CMD but keep its argument, eg @chapter."
  223.   (goto-char (point-min))
  224.   (while
  225.       (re-search-forward
  226.        (concat "^@" cmd "[ \n]+") (point-max) t)
  227.     (delete-region (match-beginning 0) (match-end 0))))
  228.  
  229. ;;; 4. @bye and other line commands without args.
  230.  
  231. (defvar detexinfo-line-cmds-without-arg
  232.   '("bye" "contents" "display" "example" "finalout" 
  233.     "flushleft" "flushright" "format" "group" "ifhtml" "ifinfo" "iftex" 
  234.     "ignore" "lisp" "menu" "noindent" "page" "quotation"  
  235.     "shortcontents" "smallbook" "smallexample" "smalllisp" 
  236.     "summarycontents" "tex" "thischapter" "thischaptername" 
  237.     "thisfile" "thispage" "thissection" "thistitle" "titlepage")
  238.   "List of Texinfo commands without arguments that should be deleted.")
  239.  
  240. (defun detexinfo-remove-line-cmds-without-arg ()
  241.   "Delete line Texinfo commands that lack args, eg. @example."
  242.   (message "Removing commands such as @example...that lack arguments...")
  243.   (mapcar 'detexinfo-remove-whole-line
  244.           detexinfo-line-cmds-without-arg))
  245.  
  246. ;;; 5. @equiv and other inline cmds without args.
  247.  
  248. (defvar detexinfo-inline-cmds-without-arg
  249.   '("equiv" "error" "expansion" "point" "print" "result"
  250.     "asis"  "br" "bullet" "dots" "minus" "today")
  251.   "List of Texinfo inline commands without arguments that should be deleted.")
  252.  
  253. (defun detexinfo-remove-inline-cmds-without-arg ()
  254.   "Delete Texinfo inline commands in that lack arguments."
  255.   (message "Removing within line commands such as @result...")
  256.   (mapcar 'detexinfo-remove-inline-cmd
  257.           detexinfo-inline-cmds-without-arg))
  258.  
  259. ;;; 6. @code and other inline cmds with args to keep
  260.  
  261. (defvar detexinfo-inline-cmds-keep-arg
  262.   '("b" "cartouche" "cite" "code" "copyright" "ctrl" "dfn" "dmn"
  263.     "emph" "file" "footnote" "i" "inforef"
  264.     "kbd" "key" "pxref" "r" "ref" "samp" "sc" "titlefont" 
  265.     "strong" "t" "var" "w" "xref")
  266.   "List of Texinfo inline commands with arguments that should be kept.")
  267.  
  268. (defun detexinfo-remove-inline-cmds-keep-arg ()
  269.   "Delete Texinfo inline commands but keep its arg, eg. @code."
  270.   (message
  271.    "Removing within line commands such as @code...but not their arguments...")
  272.   (mapcar 'detexinfo-remove-inline-cmd
  273.           detexinfo-inline-cmds-keep-arg))
  274.  
  275. ;;;;;;;;;;;;;;;; end detexinfo.el ;;;;;;;;;;;;;;;;
  276.